package com.seckill.common.config;

import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;

/**
 * 定制化tomcat参数
 * 当Spring容器内没有TomcatEmbeddedServletContainerFactory这个bean时，会把此bean加载进来
 *
 * @author lill
 * @date 2019-06-27
 */
public class WebServerConfig implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        // 使用对应工厂类提供的接口定制化 tomcat connect
        ((TomcatServletWebServerFactory) factory).addConnectorCustomizers(
               /* new TomcatConnectorCustomizer() {
                    @Override
                    public void customize(Connector connector) {

                    }
                };*/
                connector -> {
                    Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                    // 30秒没有请求，服务端自动断开keepAlive连接
                    protocol.setKeepAliveTimeout(300000);
                    // 当客户端发送超过10000个请求，服务端自动断开keepAlive连接
                    protocol.setMaxKeepAliveRequests(10000);
                    // 最大连接数，可在application中进行配置
                    // protocol.setMaxConnections();
                }
        );
    }
}
